package com.mine.yyb.modules.sys.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mine.yyb.common.utils.exception.MineErrorCode;
import com.mine.yyb.common.utils.exception.MineException;
import com.mine.yyb.modules.sys.bo.CnamzReportBO;
import com.mine.yyb.modules.sys.bo.ReportBO;
import com.mine.yyb.modules.sys.dao.IReportDao;
import com.mine.yyb.modules.sys.service.IReportService;
import com.mine.yyb.modules.sys.vo.ReportVO;

@Service(value = "iReportService")
public class ReportServiceImpl implements IReportService
{
    private static Logger logger = LoggerFactory.getLogger(ReportServiceImpl.class);
    
    @Autowired
    private IReportDao iReportDao;
    
    @Override
    public String saveReport(ReportBO reportBO)
        throws MineException
    {
        try
        {
            logger.info(reportBO.toString());
            
            ReportVO reportVO = new ReportVO();
            reportVO.setActivity(reportBO.getActivity().replaceAll("\"", ""));
            reportVO.setOrderDate(reportBO.getOrderDate().replaceAll("\"", ""));
            reportVO.setOrderID(reportBO.getOrderID().replaceAll("\"", ""));
            reportVO.setOrderPrice(Double.parseDouble(reportBO.getOrderPrice().replaceAll("\"", "")));
            reportVO.setRebate(Double.parseDouble(reportBO.getRebate().replaceAll("\"", "")));
            reportVO.setLabel(reportBO.getLabel().replaceAll("\"", ""));
            reportVO.setLabelTo(reportBO.getLabelTo().replaceAll("\"", ""));
            reportVO.setOrderIP(reportBO.getOrderIP().replaceAll("\"", ""));
            
            int result = iReportDao.save(reportVO);
            
            return result > 0 ? MineErrorCode.SUCCESS.name() : MineErrorCode.ERROR_INTERNAL.name();
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public String saveCnamzReport(CnamzReportBO reportBO)
        throws MineException
    {
        try
        {
            logger.info(reportBO.toString());
            
            ReportVO reportVO = new ReportVO();
            
            String orderDate = reportBO.getOrderDate().replaceAll("\"", "");
            Calendar c = new GregorianCalendar(1900, 0, -1);
            Date date = DateUtils.addDays(c.getTime(), Integer.parseInt(orderDate));
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            reportVO.setOrderDate(format.format(date));
            reportVO.setOrderID(reportBO.getOrderID().replaceAll("\"", ""));
            reportVO.setOrderPrice(Double.parseDouble(reportBO.getPrice().replaceAll("\"", "")));
            
            int result = iReportDao.save(reportVO);
            
            return result > 0 ? MineErrorCode.SUCCESS.name() : MineErrorCode.ERROR_INTERNAL.name();
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public String updateReport(ReportBO reportBO)
        throws MineException
    {
        try
        {
            logger.info(reportBO.toString());
            
            ReportVO reportVO = new ReportVO();
            reportVO.setActivity(reportBO.getActivity().replaceAll("\"", ""));
            reportVO.setOrderDate(reportBO.getOrderDate().replaceAll("\"", ""));
            reportVO.setOrderID(reportBO.getOrderID().replaceAll("\"", ""));
            reportVO.setOrderPrice(Double.parseDouble(reportBO.getOrderPrice().replaceAll("\"", "")));
            reportVO.setRebate(Double.parseDouble(reportBO.getRebate().replaceAll("\"", "")));
            reportVO.setLabel(reportBO.getLabel().replaceAll("\"", ""));
            reportVO.setLabelTo(reportBO.getLabelTo().replaceAll("\"", ""));
            reportVO.setOrderIP(reportBO.getOrderIP().replaceAll("\"", ""));
            
            int result = iReportDao.update(reportVO);
            return result > 0 ? MineErrorCode.SUCCESS.name() : MineErrorCode.ERROR_INTERNAL.name();
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public String updateCnamzReport(CnamzReportBO reportBO)
        throws MineException
    {
        try
        {
            logger.info(reportBO.toString());
            
            ReportVO reportVO = new ReportVO();
            
            String orderDate = reportBO.getOrderDate().replaceAll("\"", "");
            Calendar c = new GregorianCalendar(1900, 0, -1);
            Date date = DateUtils.addDays(c.getTime(), Integer.parseInt(orderDate));
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            reportVO.setOrderDate(format.format(date));
            reportVO.setOrderID(reportBO.getOrderID().replaceAll("\"", ""));
            reportVO.setOrderPrice(Double.parseDouble(reportBO.getPrice().replaceAll("\"", "")));
            
            int result = iReportDao.update(reportVO);
            return result > 0 ? MineErrorCode.SUCCESS.name() : MineErrorCode.ERROR_INTERNAL.name();
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public ReportVO queryReport(ReportBO reportBO)
        throws MineException
    {
        try
        {
            logger.info(reportBO.toString());
            
            ReportVO reportVO = new ReportVO();
            reportVO.setOrderID(reportBO.getOrderID().replaceAll("\"", ""));
            
            return iReportDao.queryReport(reportVO);
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public ReportVO queryCnamzReport(CnamzReportBO reportBO)
        throws MineException
    {
        try
        {
            logger.info(reportBO.toString());
            
            ReportVO reportVO = new ReportVO();
            reportVO.setOrderID(reportBO.getOrderID().replaceAll("\"", ""));
            
            return iReportDao.queryReport(reportVO);
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
}
